package com.itheima.contact.web;

import com.github.pagehelper.PageInfo;
import com.itheima.contact.domain.User;
import com.itheima.contact.service.UserService;
import com.itheima.contact.utils.CommonUtils;
import net.sf.jsqlparser.expression.UserVariable;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author ${Azer}
 * @LOVE,,,
 * @Date 2021/9/20 8:46
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取浏览器的隐藏的method,对应的具体方法
        String method = request.getParameter("method");

        if("addUser".equals(method)){
            //调用方法
            addUser(request, response);
        }else if("deleteUserById".equals(method)){
            deleteUserById(request, response);
        }else if("search".equals(method)){
            searchUserByCondition(request, response);

        }else if ("PageHelperSer".equals(method)){
            PageHelperSer(request, response);
        }
    }

    public void PageHelperSer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取当前页码
        String currentPage = request.getParameter("currentPage");

        int curPage = 1;    //第一次获取用户数据时，没有发送当前页码 (初始化为1)


        if(currentPage!=null){
            curPage=Integer.parseInt(currentPage);
        }

        //实例化Service层
        UserService userService=new UserService();
        //调用业务层功能
        PageInfo<User> pageInfo = userService.findUserByPage(curPage);

        //添加到域对象中
        request.setAttribute("pageInfo",pageInfo);

        //转发
        request.getRequestDispatcher("/page2.jsp").forward(request,response);

    }


        public void  searchUserByCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取表单提交的数据
        String keyword = request.getParameter("keyword");
        System.out.println("keyword : " + keyword);

        //实例化service层调用方法
        UserService userService = new UserService();
        List<User> users = userService.findUserBySearch(keyword);

        //把用户数据存储到request域对象中
        request.setAttribute("userlist", users);

        //请求转发
        request.getRequestDispatcher("/search.jsp").forward(request, response);

    }

        private void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //从浏览器获取要删除的id
        int userid =Integer.parseInt(request.getParameter("id"));
            System.out.println("要删除的用户编号："+userid);


//        System.out.println("用户id:"+userid);

        //实例化业务层
        UserService userService=new UserService();

        boolean result = userService.deleteUserById(userid);

        if (result){
            //删除成功
            response.sendRedirect("/delete_ok.jsp");
        }else {
            //删除失败
            response.getWriter().println("<h1>删除失败！3秒后跳转首页。。。</h1>");
            response.setHeader("refresh","3;url=//localhost:8080/index.jsp");
        }

    }
    /**
     * 添加用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //使用重写的工具类 来优化代码
        User user = CommonUtils.CommonPopulate(User.class,request);

        //实例化业务层
        UserService userService=new UserService();
        //调用方法
        boolean result = userService.saveUser(user);
        if(result){
            //添加成功
            response.sendRedirect("/add_ok.jsp");
        }else {
            //添加失败
            //把失败信息存储到request域对象中
            request.setAttribute("error","邮箱重复");
            request.getRequestDispatcher("/add.jsp").forward(request,response);
        }
    }
}
